import { getToken, setToken, removeToken } from '@/utils/auth.js'
import { getUSerProfiles, getUSerProfilesId } from '@/api/user'
export default {
  namespaced: true,
  state: {
    token: getToken() || '',
    // 用户信息
    userInfo: {}
  },
  mutations: {
    // token存储
    updataToken(state, token) {
      state.token = token
      setToken(token)
    },
    // token 清空
    delToken(state) {
      state.token = ''
      removeToken()
    },
    // 更新用户信息
    updataUserInfo(state, info) {
      state.userInfo = info
    },
    // 清空用户信息
    delUserInfo(state) {
      state.userInfo = {}
    }
  },
  actions: {
    async  getUserInfo(context) {
      try {
        const res = await getUSerProfiles()
        const headInfo = await getUSerProfilesId(res.data.userId)
        // console.log(headInfo)
        context.commit('updataUserInfo', { ...res.data, ...headInfo.data })
        return res.data.roles.menus
      } catch (error) {
        console.log(error)
      }
    }
  }
}

